<?php
namespace lib;

/**
 * 视图
 */
class View
{
	private $viewName;
	private $data;

           /**
            * 构造函数
            *
            * @param $viewName 视图名称
            * @param $data array 包含数据的关联数组，例如：$data['foo'] = 9, 在视图文件当中，可以使用$foo作为视图变量 $data['foo']
            */
	public function __construct($viewName, Array $data = [])
	{
		$this->viewName = $viewName;
		$this->data = $data;
	}

	public function with($name, $value) 
	{
		$this->data[$name] = $value;
		return $this;
	}

	public function render()
	{
		foreach ($this->data as $key => $value) {
			$$key = $value;
		}
		require VIEW_DIR . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $this->viewName)  . '.view.php';
	}
}